import { sortChildren } from '../utils/vnodes'
export function ChildrenMixin(_parent, options) {
  var _inject, _computed

  if (options === void 0) {
    options = {}
  }

  var indexKey = options.indexKey || 'index'
  return {
    inject:
      ((_inject = {}),
      (_inject[_parent] = {
        default: null
      }),
      _inject),
    computed:
      ((_computed = {
        parent: function parent() {
          if (this.disableBindRelation) {
            return null
          }

          return this[_parent]
        }
      }),
      (_computed[indexKey] = function() {
        this.bindRelation()

        if (this.parent) {
          return this.parent.children.indexOf(this)
        }

        return null
      }),
      _computed),
    watch: {
      disableBindRelation: function disableBindRelation(val) {
        if (!val) {
          this.bindRelation()
        }
      }
    },
    mounted: function mounted() {
      this.bindRelation()
    },
    beforeDestroy: function beforeDestroy() {
      var _this = this

      if (this.parent) {
        this.parent.children = this.parent.children.filter(function(item) {
          return item !== _this
        })
      }
    },
    methods: {
      bindRelation: function bindRelation() {
        if (!this.parent || this.parent.children.indexOf(this) !== -1) {
          return
        }

        var children = [].concat(this.parent.children, [this])
        sortChildren(children, this.parent)
        this.parent.children = children
      }
    }
  }
}
export function ParentMixin(parent) {
  return {
    provide: function provide() {
      var _ref = {}
      _ref[parent] = this
      return _ref
    },
    data: function data() {
      return {
        children: []
      }
    }
  }
}
